package com.etymgiko.spaceshipshop.web;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import com.etymgiko.spaceshipshop.service.SpaceShipManager;
import com.etymgiko.spaceshipshop.model.User;
import com.etymgiko.spaceshipshop.model.SpaceShip;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Creates new ship.
 *
 * @author Ivan Holub
 */
public class NewShipAction extends Action {
    private SpaceShipManager spaceShipManager;

    public void setSpaceShipManager(SpaceShipManager spaceShipManager) {
        this.spaceShipManager = spaceShipManager;
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

        NewShipForm newShipForm = (NewShipForm) form;

        String name = newShipForm.getName();
        String typename = newShipForm.getTypename();
        String manufacturer = newShipForm.getManufacturer();

        SpaceShip ship = new SpaceShip();
        ship.setName(name);
        ship.setTypename(typename);
        ship.setManufacturer(manufacturer);

        spaceShipManager.saveSpaceShip(ship);

        return mapping.findForward("success");
    }
}
